package com.heima.article.controller.v1;


import com.heima.apis.article.ArticleControllerApi;
import com.heima.common.article.ArticleConstans;
import com.heima.article.feign.WemediaFeign;
import com.heima.model.article.dto.ArticleHomeDto;
import com.heima.model.article.dto.ArticleInfoDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.media.dto.NewsDto;
import com.heima.article.service.ApArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/v1/article")
public class ArticleController implements ArticleControllerApi {

    @Autowired
    ApArticleService articleService;


    /**
     * 根据id查询
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/getById")
    public ResponseResult getById(@RequestBody ArticleInfoDto dto) {

        return articleService.findById(dto);
    }

    /**
     * 修改文章上下架，只用做feign远程调用，不对外暴露
     * 配合自媒体端完成上下架同步更新
     * @param dto
     * @return
     */
    @PutMapping("/downOrUp")
    @Override
    public ResponseResult downOrUp(@RequestBody NewsDto dto) {
        return articleService.downOrUp(dto);
    }

    @Autowired
    WemediaFeign wemediaFeign;

    /**
     * 文章上架和下架，对外暴露
     * @param dto
     * @return
     */
    @PutMapping("/isDown")
    @Override
    public ResponseResult isDown(@RequestBody NewsDto dto) {
        return wemediaFeign.downOrUp(dto);
    }


    /**
     * 分页条件查询，ap端默认查询
     * @param dto
     * @return
     */
    @PostMapping("/load")
    @Override
    public  ResponseResult<List<ApArticle>> findAll(@RequestBody ArticleHomeDto dto) {
        return articleService.load(ArticleConstans.LOADTYPE_LOAD_MORE,dto);
    }

    /**
     * 加载最新
     * @param dto
     * @return
     */
    @PostMapping("/loadNew")
    @Override
    public ResponseResult<List<ApArticle>> loadNew(@RequestBody ArticleHomeDto dto) {
        return articleService.load(ArticleConstans.LOADTYPE_LOAD_MORE,dto);
    }


    /**
     * 加载最新
     * @param dto
     * @return
     */
    @PostMapping("/loadMore")
    @Override
    public  ResponseResult<List<ApArticle>> loadMore(@RequestBody ArticleHomeDto dto) {
        return articleService.load(ArticleConstans.LOADTYPE_LOAD_NEW,dto);
    }

    /**
     * 查询文章相关信息
     * @param dto
     * @return
     */
    @PostMapping("/loadArticleBehavior")
    @Override
    public ResponseResult loadArticleBehavior(@RequestBody ArticleInfoDto dto) {

        return articleService.loadArticleBehavior(dto);
    }

    /**
     * 根据文章id查询，并返回
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @Override
    public ResponseResult<ApArticle> findById(@PathVariable("id") Long id) {
        return ResponseResult.okResult(articleService.getById(id));
    }

}
